<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {
  orderUser
} from "@/api/business/order";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Number,
      default: 7
    }
  },
  data() {
    return {
      chart: null,
      xData: [],
      yData: []
    }
  },
  created() {
    this.orderChart(this.chartData);
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.orderChart(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    orderChart(num) {
      orderUser(num).then(response => {
        this.xData = response.xData
        this.yData = response.yData
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        title: {
          text: '用户报单量'
        },
        xAxis: {
          type: 'category',
          data: this.xData
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.yData,
            type: 'bar',
            barWidth: 35
          }
        ]
      })
    }
  }
}
</script>
